package com.spring.mvc.controller;

import com.spring.mvc.dao.EmployeeDao;
import com.spring.mvc.pojo.Employee;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.Collection;
import java.util.Date;

/**
 * Created by Admin on 9/26 0026.
 */
@Controller
public class Test {
    @Autowired
    private EmployeeDao employeeDao;
    @ResponseBody
    @RequestMapping("/testJson")
    public Collection<Employee> getEmployee(){
        return employeeDao.getAll();
    }

    @ResponseBody
    @RequestMapping("/testMassageConverter")
    public String testMassageConverter(@RequestBody String body){
        System.out.println("*******body: "+body);
        return "hellotestMassageConverter"+new Date();
    }

    /**
     *下载
     * @param session
     * @return
     * @throws IOException
     */
    @RequestMapping("/testResponseEntity")
    public ResponseEntity<byte[]> testResponseEntity(HttpSession session) throws IOException {
        byte[] bytes=null;
        ServletContext servletContext=session.getServletContext();
        InputStream in=servletContext.getResourceAsStream("/files/abc.txt");
        bytes=new byte[in.available()];
        in.read(bytes);
        HttpHeaders httpHeaders=new HttpHeaders();
        httpHeaders.add("Content-Disposition","attachment;filename=abc.txt");
        HttpStatus httpStatus=HttpStatus.OK;
        ResponseEntity<byte[]> responseEntity=new ResponseEntity<byte[]>(bytes,httpHeaders,httpStatus);
        return responseEntity;
    }

    @RequestMapping("/testDownload")
    public ResponseEntity<byte[]> testDownload() throws IOException {
        String path="C:\\Users\\Admin\\Desktop\\abc.txt";
        HttpHeaders httpHeaders=new HttpHeaders();
        //为了解决中文乱码问题;
//        String fileName=new String ("aaa.txt".getBytes("utf-8"),"iso-8859-1");
//        httpHeaders.setContentDispositionFormData("attachment",fileName);
        //filename:保存到本地文件的名字;
        httpHeaders.add("Content-Disposition","attachment;filename=aaa.txt");
        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(new File(path)),httpHeaders,HttpStatus.OK);
    }

    /**
     * 文件上传
     * @param desc
     * @param file
     * @return
     * @throws IOException
     */
    @RequestMapping("/testMultipartResolver")
    public String testMultipartResolver(@RequestParam("desc") String desc,
                                        @RequestParam("file") MultipartFile file) throws IOException {
        System.out.println("desc: "+desc);
        System.out.println("file: "+ file.getOriginalFilename());
        System.out.println("file: "+ file.getName());
        System.out.println("file: "+ file.getContentType());
        System.out.println("file: "+ file.getBytes());
        System.out.println("file: "+ file.getSize());
        System.out.println("fileType: "+file.getInputStream());

        return "success";
    }

    @RequestMapping("/testException")
    public String testException(@RequestParam("i") int i){
        System.out.println("erroe: "+(10/i));
        return "success";
    }

    /**
     * 1.在@ExceptionHandler 方法的入参中可以加入Exception 类型的参数;该参数即对应发生的异常类;
     * 2.在@ExceptionHandler 方法的入参中不能加入Map,要想使得错误信息打印到页面,就需要使用ModelAndView
     * 3.@ExceptionHandler 标记优先级的问题: 越靠近实际的异常类信息,先执行;
     * 4.@ControllerAdvice : 如果在当前的Handler 中找不到@ExceptionHandler标记的方法来处理异常;
     * 那么就去@ControllerAdvice 中找标记有@ExceptionHandler 的方法来处理异常;
     */
    @ExceptionHandler({ArithmeticException.class})
    public ModelAndView testException(Exception ex){
        System.out.println("error---1: "+ex);
        ModelAndView mv=new ModelAndView("error");
        mv.addObject("exception",ex);
        return mv;
    }

    @ExceptionHandler({RuntimeException.class})
    public ModelAndView testException2(Exception ex){
        System.out.println("error---2: "+ex);
        ModelAndView mv=new ModelAndView("error");
        mv.addObject("exception",ex);
        return mv;
    }


    @RequestMapping("/testSimpleMappingException")
    public String testSimpleMappingException(@RequestParam("i") int i){
        String[] values=new String[10];
        System.out.println(values[i]);
        return "success";
    }

}
